package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysGroundDao;
import com.gusha.model.ReceivingAddress;
import com.gusha.model.SysGround;
import com.gusha.model.User;

@Service
@Transactional
public class SysGroundService {
	@Autowired
	private SysGroundDao sysGroundDao;
	
	public Page<SysGround> findAll(Pageable pageable, SysGround sysGround) {
        @SuppressWarnings("serial")
        Page<SysGround> result = sysGroundDao.findAll(new Specification<SysGround>() {
            @Override
            public Predicate toPredicate(Root<SysGround> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysGround.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysGround.getId()));
                }
                if (sysGround.getUserId() != null) {
                    list.add(cb.equal(root.get("userId").as(User.class),sysGround.getUserId()));
                }
                if (sysGround.getGrAcreage() != null) {
                    list.add(cb.equal(root.get("grAcreage").as(BigDecimal.class),sysGround.getGrAcreage()));
                }
                if (sysGround.getGrTerm() != null) {
                    list.add(cb.equal(root.get("grTerm").as(BigDecimal.class),sysGround.getGrTerm()));
                }
                if (sysGround.getGrPrice() != null) {
                    list.add(cb.equal(root.get("grPrice").as(BigDecimal.class),sysGround.getGrPrice()));
                }
                if (sysGround.getGrAddress() != null) {
                    list.add(cb.equal(root.get("grAddress").as(String.class),sysGround.getGrAddress()));
                }
                if (sysGround.getPaymentTime() != null) {
                    list.add(cb.equal(root.get("paymentTime").as(Date.class),sysGround.getPaymentTime()));
                }
                if (sysGround.getPaymentState() != null) {
                    list.add(cb.equal(root.get("paymentState").as(BigInteger.class),sysGround.getPaymentState()));
                }
                if (sysGround.getAddress() != null) {
                    list.add(cb.equal(root.get("address").as(ReceivingAddress.class),sysGround.getAddress()));
                }
                if (sysGround.getGroundType() != null) {
                    list.add(cb.equal(root.get("groundType").as(BigInteger.class),sysGround.getGroundType()));
                }
               
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
